﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;

/// <summary>
/// Summary description for Ve
/// </summary>
public class Ve:DBHelper
{
	public Ve()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    public IQueryable<veonline> GetData()
    {
        var db = new LanAnhDataContext(ConnectionString);
        var ve = db.veonlines.Select(v => v).OrderByDescending(v => v.ngaydat);
        return ve;
    }
    public void DeleteVe(int mave)
    {
        var db = new LanAnhDataContext(ConnectionString);
        //veonline ve = db.veonlines.Single(x => x.mave == mave);
        //db.veonlines.DeleteOnSubmit(ve);
        //db.SubmitChanges();
        db.sp_HuyDatVe(mave);
    }
    public void UpdateTrangThaiVe(int mave, string trangthai)
    {
        var db = new LanAnhDataContext(ConnectionString);
        //veonline ve = db.veonlines.Single(x => x.mave == mave);
        //ve.trangthai = trangthai;       
        //db.SubmitChanges();
        db.sp_CapNhatTrangThaiVe_UnrepeatableRead(mave,trangthai);       
    }
    public static IEnumerable ThongKe(DateTime from, DateTime to)
    {
        var db = new LanAnhDataContext(ConnectionString);
        var thongke = db.sp_ThongKe(from, to);
        return thongke.ToList();
    }
    public static IEnumerable GetDataLoaiGhe()
    {
        var db = new LanAnhDataContext(ConnectionString);
        var ghe = from g in db.loaighes
                 select g;
        return ghe;
    }
    public static void GiamGiaVe(int phantram, string maloaighe)
    {
        var db = new LanAnhDataContext(ConnectionString);
        if (check == 1)
        {
            db.sp_GiamGiaVe_Error_Deadlock(phantram, maloaighe);
        }
        else
        {
            db.sp_GiamGiaVe_Fix_Deadlock(phantram, maloaighe);
        }
    }
    public static IEnumerable ThongKeVe(string taikhoan,ref string message)
    {
        using (var db = new LanAnhDataContext(ConnectionString))
        {

            if (check == 1)
            {
                return db.sp_DanhSachVeCuaTaiKhoan_Error_UnrepeatableRead(taikhoan,ref message).ToList();               
            }
            else
            {
                return db.sp_DanhSachVeCuaTaiKhoan_Fix_UnrepeatableRead(taikhoan,ref message).ToList();
            } 
           
        }
    }

    public static void datVe(DateTime ngaydat, string trangthai, string taikhoan, string msghe, int mald)
    {
        using(var db = new LanAnhDataContext(ConnectionString))
        {
            try
            {
                db.sp_DatVe(ngaydat, trangthai, taikhoan, msghe, mald);
            }
            catch (Exception)
            {
                throw;
            }
        }
    }

    public static string[] GetVeByLichDien(int mald)
    {
        using (var db = new LanAnhDataContext(ConnectionString))
        {
            var list = from ve in db.veonlines
                       where ve.mald == mald
                       select (ve.masoghe);
            return list.ToArray();
        }
    }
}
